import { SliderField } from '@aws-amplify/ui-react'; import { SliderFieldDemo } from './demo'; import { DefaultSliderFieldExample, ControlledSliderFieldExample, SliderFieldBasicsExample, SliderFieldFormatValueExample, SliderFieldOrientationExample, SliderFieldAriaExample, SliderFieldIconsExample, SliderFieldAccessibilityExample, SliderFieldFormExample, SliderFieldValidationExample, SliderFieldThemeExample, SliderFieldStylePropsExample, } from './examples'; import { Example, ExampleCode } from '@/components/Example'; import { Fragment } from '@/components/Fragment'; import { ComponentStyleDisplay } from '@/components/ComponentStyleDisplay'; import ThemeExample from '@/components/ThemeExample.mdx'; ## Demo ## Usage Import the SliderField component. ```jsx file=./examples/DefaultSliderFieldExample.tsx ```` ### Controlled component To use the SliderField as a controlled component, handle the current value using the `value` and `onChange` props. ```jsx file=./examples/ControlledSliderFieldExample.tsx ```` ### Basics To control the range of the SliderField, use the `min` and `max` props. To control the interval between selectable values, use the `step` prop (defaults to 1). You may also set a `defaultValue`. ```jsx file=./examples/SliderFieldBasicsExample.tsx ```` ### Accessibility / Label behavior {() => import('./../shared/formFieldAccessibility.mdx')} You can hide the value to the right of the label with `isValueHidden` prop. ```jsx file=./examples/SliderFieldAccessibilityExample.tsx ```` The SliderField will programmatically update the value of `aria-valuenow` in response to user input. However, a slider sometimes is used to choose a value that is not, semantically, a number. In these cases, the `ariaValuetext` attribute is used to provide the appropriate text name for the currently selected value. See [MDN using the slider role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/slider_role). ```jsx file=./examples/SliderFieldAriaExample.tsx ```` ### Forms To use the SliderField in a form, pass a `name` prop to the SliderField to access its current value. To disable the SliderField, set the `isDisabled` prop. A disabled SliderField will not be focusable, mutable, or submit a value with form data. ```jsx file=./examples/SliderFieldFormExample.tsx ```` ### Orientation To change the orientation from horizontal (default) to vertical, use the `orientation` prop. ```jsx file=./examples/SliderFieldOrientationExample.tsx ```` ### Icons To add icons on either side of the SliderField, you may use the `outerStartComponent` or `outerEndComponent` props. ```jsx file=./examples/SliderFieldIconsExample.tsx ```` ### Format value To format how the `value` is displayed, you can pass in a render function to `formatValue` prop. ```jsx file=./examples/SliderFieldFormatValueExample.tsx ``` ### Validation error To validate the SliderField input, use the `hasError` and `errorMessage` props. ```jsx file=./examples/SliderFieldValidationExample.tsx ```` ## CSS Styling ```tsx file=./examples/SliderFieldThemeExample.tsx ``` ### Target classes ### Global styling To override styling on all SliderField components, you can set the Amplify CSS variables or use the target classes. ```css /* styles.css */ .amplify-sliderfield { --amplify-components-sliderfield-range-background-color: var( --amplify-colors-orange-60 ); } ``` ### Local styling To override styling on a specific SliderField, you can use class selectors or style props. _Using a class selector:_ Note that `.amplify-sliderfield__range` applies to the filled-in portion of the SliderField track, and `.amplify-sliderfield__track` applies to the empty portion. ```css /* styles.css */ .custom-slider .amplify-sliderfield__track { background-color: var(--amplify-colors-purple-80); } ``` ```jsx import { SliderField } from '@aws-amplify/ui-react'; import './styles.css'; ; ``` _Using style props:_ There are several props you can use to style different parts of the SliderField: - `filledTrackColor` applies to the filled-in part of the SliderField - `emptyTrackColor` applies to the empty part of the SliderField - `thumbColor` applies to the thumb component that users can slide - `trackSize` applies to the width of the track itself (e.g., 15px) - `size` applies to the overall size of the SliderField, including the thumb (options include `'small'`, `'large'`, and default) ```jsx file=./examples/SliderFieldStylePropsExample.tsx ```